package chat02;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2024-09-12
 * Time: 20:24
 */
public class ServerThread extends  Thread{

    private Socket socket;

    public ServerThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        while(true){
            System.out.println("连接成功");

            InputStream inputStream = null;
            OutputStream outputStream = null;

            byte[] b = new byte[1024];

            int len;

            try {
                inputStream = socket.getInputStream();

                while (((len=inputStream.read(b))!=-1)){

                    for (Socket socket1 : Server.list) {
                        if(socket1 != socket && !socket1.isClosed()){
                            outputStream = socket1.getOutputStream();
                            String s = new String(b, 0, len);
                            outputStream.write(s.getBytes());
                            outputStream.flush();
                            System.out.println("服务器回复消息成功");
                        }
                    }
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
